# coding:utf-8
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import KernelDensity
from scipy import integrate
import pandas as pd
import pickle
from datetime import datetime

print("first we load data and sort it")
data = pd.read_table("../orders.tbl", sep='|')

data.sort_values('o_totalprice', inplace=True)
data.reset_index(drop=True, inplace=True)

print("finish data preprocess")

# kde = KernelDensity(kernel='gaussian', bandwidth=2).fit(X)

with open('../kde.pkl', 'rb') as f:
    kde = pickle.load(f)


def f_p(*args):
    return np.exp(kde.score_samples(np.array(args).reshape(1, -1)))


count = 0
result_list = []
time_list = []

start = datetime.now()

total_min = data['o_totalprice'].min()
total_max = data['o_totalprice'].max()
x = total_min
ans = float(0)

data = data.values

for row in data:
    pre_x = x
    x = row[3]

    if abs(x - pre_x) > (row[3] / float(10)):
        # if pre_x_min != x_min:
        #     a = integrate.quad(f_p, pre_x_min, x_min, epsabs=10.0, epsrel=0.1)[0] * float(1500000)
        # else:
        #     a = float(0)
        # if pre_x_max != x_max:
        #     b = integrate.quad(f_p, pre_x_max, x_max, epsabs=10.0, epsrel=0.1)[0] * float(1500000)
        # else:
        #     b = float(0)
        # ans = ans - a + b
        ans = integrate.quad(f_p, total_min, x, epsabs=10.0, epsrel=0.1)[0]
    else:
        x = pre_x

    result_list.append(ans)
    count = count + 1

end = datetime.now()
time_cost = (end - start).total_seconds()
print("time cost {}".format(time_cost))

result_list = np.array(result_list)

# result.tofile("result.txt")

print("now we calculate avg relative error")
standard_result = pd.read_csv("range_cumedist1g.csv")['no']
standard_result = standard_result.values
relative_error = (abs(standard_result - result_list) / standard_result).mean() * 100

print("relative error is: {}%".format(relative_error))
